<?php

namespace App\Api;
header("Access-Control-Allow-Origin: *");
header('Access-Control-Allow-Methods: GET,POST,PUT,DELETE,OPTIONS,PATCH');
header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept");

use App\Domain\User as UserDomain;
use PhalApi\Api;

/**
 * 用户模块接口服务
 */
class User extends Api
{

    public function getRules()
    {
        return array(
            'login' => array(
                'username' => array('name' => 'username', 'require' => true, 'min' => 1, 'max' => 50, 'source' => 'post', 'desc' => '用户名'),
                'password' => array('name' => 'password', 'require' => true, 'min' => 1, 'max' => 50, 'source' => 'post', 'desc' => '密码'),
            ),
            'getUserInfo' => array(
                'userid' => array('name' => 'userid', 'require' => true, 'min' => 1, 'desc' => '用户userid')
            ),
            'getUserInfoByUserName' => array(
                'user_name' => array('name' => 'user_name', 'require' => true, 'min' => 1, 'desc' => '用户账号')
            ),
            'getUserInfoByEmail' => array(
                'email' => array('name' => 'email', 'require' => true, 'min' => 1, 'max' => 30, 'desc' => '用户邮箱')
            ),
            'delUserById' => array(
                'userId' => array('name' => 'userId', 'require' => true, 'min' => 1, 'desc' => '用户userId')
            ),
            'insertUserInfo' => array(
                'username' => array('name' => 'username', 'require' => true, 'min' => 1, 'max' => 50, 'source' => 'post', 'desc' => '用户名'),
                'password' => array('name' => 'password', 'require' => true, 'min' => 1, 'max' => 50, 'source' => 'post', 'desc' => '密码'),
                'email' => array('name' => 'email', 'require' => true, 'min' => 1, 'max' => 30, 'source' => 'post', 'desc' => '邮箱'),
                'gender' => array('name' => 'gender', 'require' => true, 'min' => 1, 'max' => 3, 'source' => 'post', 'desc' => '性别'),
            ),
            'likeQueryByUserName' => array(
                'like_username' => array('name' => 'like_username', 'require' => true, 'min' => 1, 'desc' => '模糊的用户名称')
            ),
        );
    }

    /**
     * 登录接口
     * @desc 根据账号密码进行登录
     * @return boolean is_login 是否登录成功
     * @return int userid 用户ID
     */
    public function login()
    {
        $username = $_POST['username'];//带参数传输方法
        $password = $_POST['password'];//带参数传输方法
        $domain = new UserDomain();
        $info = $domain->login($username, $password);
        if ($info != null) {
            return array('is_login' => true, 'userid' => $info[0]['id'], 'username' => $info[0]['username']);
        } else {
            return array('is_login' => false);
        }

    }

    /**
     * 获取用户登录信息
     * @desc 根据用户ID获取指定用户信息
     */
    public function getUserInfo()
    {
        $userid = $_GET['userid'];//带参数传输方法
        $domain = new UserDomain();
        $info = $domain->getUserInfo($userid);

        return array('info' => $info);
    }

    /**
     * 查询用户名是否重复
     * @desc 根据用户名返回用户信息
     */
    public function getUserInfoByUserName()
    {
        $user_name = $_GET['user_name'];//带参数传输方法
        $domain = new UserDomain();
        $info = $domain->getUserInfoByUserName($user_name);
        return array('info' => $info);
    }

    /**
     * 查询邮箱是否重复
     * @desc 根据邮箱名返回用户信息
     */
    public function getUserInfoByEmail()
    {
        $email = $_GET['email'];//带参数传输方法
        $domain = new UserDomain();
        $info = $domain->getUserInfoByEmail($email);
        return array('info' => $info);
    }

    /**
     * 获取所有用户列表信息
     * @desc 获取所有用户详细信息
     */
    public function getUserList()
    {
        $domain = new UserDomain();
        $info = $domain->getUserList();
        return array('info' => $info);

    }

    /**
     * 删除指定用户id的列表信息
     * @desc 删除指定用户id的条目,返回影响行数
     *
     */
    public function delUserById()
    {
        $userId = $_GET['userId'];
        $domain = new UserDomain();
        $row = $domain->delUserById($userId);
        return array('row' => $row);

    }

    /**
     * 插入一条用户数据，根据用户名，密码，邮箱地址，和性别
     * @desc 新增一条用户记录，根据用户名，密码，邮箱地址，性别
     * 返回新增的ID（注意，这里不能使用连贯操作，因为要保持同一个ORM实例）
     */
    public function insertUserInfo()
    {
        $username = $_POST['username'];
        $password = $_POST['password'];
        $email = $_POST['email'];
        $gender = $_POST['gender'];
        $domain = new UserDomain();
        $id = $domain->insertUserInfo($username, $password, $email, $gender);
        return array('id' => $id);
    }

    /**
     * 按用户名模糊查询用户信息
     * @desc 传用户名的相关信息，查询相匹配的用户，实现模糊查询
     */
    public function likeQueryByUserName()
    {
        $like_username = $_GET['like_username'];
        $domain = new UserDomain();
        $info = $domain->likeQueryByUserName($like_username);
        return array('info' => $info);
    }
}